package com.dhcc.DRGsHosp.web.controller;

import com.dhcc.DRGsHosp.facade.ClinicGroupFacade;
import com.dhcc.DRGsHosp.facade.dto.ClinicGroupDTO;
import org.codehaus.jackson.map.ObjectMapper;
import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller("drgsHospWebControllerchl2017051101")
@RequestMapping("/ClinicGroup")
public class ClinicGroupController {
		
	@Inject
	private ClinicGroupFacade clinicGroupFacade;
	
	@ResponseBody
	@RequestMapping("/add")
	public InvokeResult add(String param) {
		ObjectMapper objectMapper = new ObjectMapper();
		Map<String, Object> entity;
		try {
			entity = objectMapper.readValue(param, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
			entity = new HashMap<String, Object>();
		}
		return clinicGroupFacade.saveClinicGroup(entity);
	}
	
	@ResponseBody
	@RequestMapping("/pageJson")
	public Page pageJson(ClinicGroupDTO clinicGroupDTO, @RequestParam int page, @RequestParam int pagesize) {
		Page<ClinicGroupDTO> all = clinicGroupFacade.pageQueryClinicGroup(clinicGroupDTO, page, pagesize);
		return all;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public InvokeResult remove(@RequestParam String ids) {
		String[] value = ids.split(",");
        String[] idArrs = new String[value.length];
        for (int i = 0; i < value.length; i ++) {
        	idArrs[i] = value[i] ;
		}
        return clinicGroupFacade.removeClinicGroups(idArrs);
	}
	
	@ResponseBody
	@RequestMapping("/get/{id}")
	public InvokeResult get(@PathVariable Long id) {
		return clinicGroupFacade.getClinicGroup(id);
	}
	
		

    @ResponseBody
  	@RequestMapping("/findAllClinicGroup")
      public InvokeResult findAllClinicGroup(ClinicGroupDTO dto){
      	return InvokeResult.success(clinicGroupFacade.findAllClinicGroup(dto));
      }
    
    
    /**
     *  医师编码或显示顺序是否存在
     *  isGroupCodeOrOrderExist.  
     *  @Description：
     *  @Author ChenHanlin
     *  @param groupCode
     *  @param groupOrder
     *  @return 
     *  @Date:2017年12月15日上午10:03:44
     */
    @ResponseBody
  	@RequestMapping("/isGroupCodeOrOrderExist")
      public InvokeResult isGroupCodeOrOrderExist(String groupCode,String groupOrder) {
    	Map<String,Object> map = new HashMap<>() ;
    	if( null != groupOrder ) {
    		boolean flag_order = clinicGroupFacade.isGroupOrderExist(groupOrder,null) ;
    		map.put("flag_order", flag_order) ;
    	}
    	if( null != groupCode ) {
    		boolean flag_code = clinicGroupFacade.isGroupCodeExist(groupCode) ;
    		map.put("flag_code", flag_code) ;
    	}
      	return  InvokeResult.success(map);
      }
    
    /**
     *  医师编码是否存在
     *  isGroupCodeExist.  
     *  @Description：
     *  @Author ChenHanlin
     *  @param groupCode
     *  @param groupOrder
     *  @return 
     *  @Date:2017年12月15日上午10:02:13
     */
    @ResponseBody
    @RequestMapping("/isGroupCodeExist")
    public boolean isGroupCodeExist(String groupCode,String groupOrder) {
    	return  clinicGroupFacade.isGroupOrderExist(groupOrder,groupCode);
    }
    
    /**
     *   查询医师编码为空的病例数
     *  findCaseCntDocCodeIsNull.  
     *  @Description：
     *  @Author ChenHanlin
     *  @param dataFrom
     *  @param dataTo
     *  @return 
     *  @Date:2017年12月15日上午10:08:53
     */
    @ResponseBody
    @RequestMapping("/findCaseCntDocCodeIsNull")
    public InvokeResult findCaseCntDocCodeIsNull(String dateFrom,String dateTo) {
    	return  InvokeResult.success(clinicGroupFacade.findCaseCntDocCodeIsNull(dateFrom,dateTo));
    }
    
    /**
     *  同步首页和医师表中的医师信息
     *  keepIpinfoAndClinicGroup.  
     *  @Description：
     *  @Author ChenHanlin
     *  @param dataFrom
     *  @param dataTo
     *  @return 
     *  @Date:2017年12月15日上午10:32:00
     */
    @ResponseBody
    @RequestMapping("/keepIpinfoAndClinicGroup")
    public InvokeResult keepIpinfoAndClinicGroup(String dateFrom,String dateTo) {
    	clinicGroupFacade.keepIpinfoAndClinicGroup(dateFrom,dateTo) ;
    	return  InvokeResult.success();
    }
}
